<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <!--        <el-button  icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">刷新</el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="primary"-->
    <!--          plain-->
    <!--          icon="el-icon-plus"-->
    <!--          size="mini"-->
    <!--          @click="handleAdd"-->
    <!--          v-hasPermi="['business:waitlist:add']"-->
    <!--        >新增</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="success"-->
    <!--          plain-->
    <!--          icon="el-icon-edit"-->
    <!--          size="mini"-->
    <!--          :disabled="single"-->
    <!--          @click="handleUpdate"-->
    <!--          v-hasPermi="['business:waitlist:edit']"-->
    <!--        >修改</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="danger"-->
    <!--          plain-->
    <!--          icon="el-icon-delete"-->
    <!--          size="mini"-->
    <!--          :disabled="multiple"-->
    <!--          @click="handleDelete"-->
    <!--          v-hasPermi="['business:waitlist:remove']"-->
    <!--        >删除</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="warning"-->
    <!--          plain-->
    <!--          icon="el-icon-download"-->
    <!--          size="mini"-->
    <!--          @click="handleExport"-->
    <!--          v-hasPermi="['business:waitlist:export']"-->
    <!--        >导出</el-button>-->
    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <div class="table-container">
      <el-table
        v-loading="loading"
        :data="waitlistList"
        :row-style="{ height: '60px' }"
        @selection-change="handleSelectionChange"
        style="width: 100%"
        :header-cell-style="{
          background: '#F5F7FA',
          color: '#606266',
          height: '45px',
          padding: '0',
          whiteSpace: 'nowrap'
        }"
      >
        <el-table-column type="selection" width="55" align="center" min-width="140" />
        <el-table-column show-overflow-tooltip label="候补订单号" align="center" prop="id" min-width="140" />
        <el-table-column show-overflow-tooltip label="用户名" align="center" prop="memberId" min-width="140" />
        <el-table-column show-overflow-tooltip label="列车编号" align="center" prop="trainCode" min-width="140" />
        <el-table-column show-overflow-tooltip label="出发日期" align="center" prop="date" min-width="140">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip label="出发站" align="center" prop="start" min-width="140" />
        <el-table-column show-overflow-tooltip label="到达站" align="center" prop="end" min-width="140" />
        <el-table-column show-overflow-tooltip label="座位类型" align="center" prop="seatType" min-width="140">
          <template slot-scope="scope">
            <span>{{ formatSeatType(scope.row.seatType) }}</span>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip label="乘车人数量" align="center" prop="quantity" min-width="140" />
        <el-table-column show-overflow-tooltip label="出发时间" align="center" prop="startTime" min-width="140">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip label="到达时间" align="center" prop="endTime" min-width="140" />
        <el-table-column show-overflow-tooltip label="序号" align="center" prop="subscript" min-width="140" />
        <el-table-column show-overflow-tooltip label="截止兑现时间" align="center" prop="Deadline" min-width="140">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.Deadline, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip label="订单状态" align="center" prop="status" min-width="140">
          <template slot-scope="scope">
            <span>{{ formatStatus(scope.row.status) }}</span>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip label="截止支付时间" align="center" prop="timeOut" min-width="140">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.timeOut, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改候补订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWaitlist, getWaitlist, delWaitlist, addWaitlist, updateWaitlist } from "@/api/business/waitlist";

export default {
  name: "Waitlist",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 候补订单表格数据
      waitlistList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        memberId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        trainCode: [
          { required: true, message: "列车编号不能为空", trigger: "blur" }
        ],
        date: [
          { required: true, message: "出发日期不能为空", trigger: "blur" }
        ],
        start: [
          { required: true, message: "出发站不能为空", trigger: "blur" }
        ],
        end: [
          { required: true, message: "到达站不能为空", trigger: "blur" }
        ],
        seatType: [
          { required: true, message: "座位类型不能为空", trigger: "change" }
        ],
        quantity: [
          { required: true, message: "候补人信息不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "出发时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "到达时间不能为空", trigger: "blur" }
        ],
        subscript: [
          { required: true, message: "序号不能为空", trigger: "blur" }
        ],
      },
      // 添加状态映射
      statusMap: {
        'W': '候补等待中',
        'C': '已取消',
        'PAY': '待支付',
        'PG': '支付处理中',
        'PAID': '已支付',
        'REFUND': '已退款',
        'F': '候补失败',
        'PAY_FAILURE': '支付失败',
        'EXPIRE': '支付超时'
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询候补订单列表 */
    getList() {
      this.loading = true;
      listWaitlist(this.queryParams).then(response => {
        this.waitlistList = response.rows;
        this.waitlistList.map(item=>{
          return item.quantity = JSON.parse(item.quantity).length;
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        memberId: null,
        trainCode: null,
        date: null,
        start: null,
        end: null,
        seatType: null,
        quantity: null,
        startTime: null,
        endTime: null,
        createTime: null,
        subscript: null,
        accept: null,
        Deadline: null,
        status: null,
        timeOut: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 添加回车键处理
    handleKeyup(e) {
      if (e.keyCode === 13) {
        e.preventDefault();
        this.handleQuery();
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加候补订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWaitlist(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改候补订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWaitlist(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWaitlist(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除候补订单编号为"' + ids + '"的数据项？').then(function() {
        return delWaitlist(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/waitlist/export', {
        ...this.queryParams
      }, `waitlist_${new Date().getTime()}.xlsx`)
    },
    // 添加状态格式化方法
    formatStatus(status) {
      return this.statusMap[status] || status;
    },
    formatSeatType(seatType) {
      // Implement the logic to format seat type
      return seatType;
    },
    formatIdType(idType) {
      // Implement the logic to format ID type
      return idType;
    }
  },
  mounted() {
    // 添加回车键事件监听
    document.addEventListener('keyup', this.handleKeyup);
  },
  beforeDestroy() {
    // 移除事件监听
    document.removeEventListener('keyup', this.handleKeyup);
  }
};
</script>

<style scoped>
.table-container {
  width: 100%;
  overflow-x: auto;
  margin-bottom: 20px;
}
</style>
